# -*- coding: utf-8 -*-
# @Time    : 2022/7/15 15:57
# @Author  : vvh
# @Email   : 2647167477@qq.com
# @File    : text_del_model_575.py
# @Software: PyCharm
# 验证删除单个产品类别，且其不是其他类别的父类
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

# 数据
username = 'vvh'
password = '123456'
login_url = 'http://192.168.1.90/crm'
goods_model_page = 'http://192.168.1.90/crm/index.php?m=product&a=category&model=product'
# 需要删除的分类名
del_name = '--哇哈哈'

# 定位器
username_locator = (By.NAME, "name")
password_locator = (By.NAME, "password")
login_btn_locator = (By.NAME, "submit")

model_name_locator = (By.ID, 'name')
parent_class_locator = (By.ID, 'categoryList')
describe_locator = (By.XPATH,'//*[@id="dialog-message2"]/form/div[3]/div/textarea')

cancel_btn_locator = (By.XPATH,'/html/body/div[8]/div[1]/button')
# 产品分类页定位器
tbody_locator = (By.TAG_NAME,'tbody')
tr_locator = (By.TAG_NAME,'tr')
td_locator = (By.TAG_NAME,'td')
check_box_locator = (By.TAG_NAME,'input')
del_btn_locator = (By.CSS_SELECTOR,'body > div.container > div.row > form > div:nth-child(1) > div > input')
submit_btn_locator = (By.CSS_SELECTOR,'#dialog-message1 > form > div:nth-child(4) > div > input:nth-child(1)')
add_moedel_locator = (By.ID,'add_category')
# 打开浏览器
driver = webdriver.Chrome()
driver.get(login_url)
driver.maximize_window()
# 登录
driver.find_element(*username_locator).send_keys(username)
driver.find_element(*password_locator).send_keys(password)
driver.find_element(*login_btn_locator).click()
# 产品分类页
driver.get(goods_model_page)
model_lst = driver.find_element(*tbody_locator).find_elements(*tr_locator)
for row in model_lst:
    # 找到分类名所在行
    add_model = row.find_elements(*td_locator)[1]
    if del_name == add_model.text:
        row.find_elements(*td_locator)[0].find_element(By.TAG_NAME,'input').click()
        row.find_elements(*td_locator)[-1].find_element(By.LINK_TEXT,'编辑').click()
        wait = WebDriverWait(driver, 10, 0.5)
        wait.until(EC.presence_of_all_elements_located((By.ID, 'name')))
        get_model_name = driver.find_element(*model_name_locator).get_attribute('value')
        get_perent_class = Select(driver.find_element(*parent_class_locator)).first_selected_option.text
        get_descripe = driver.find_element(*describe_locator).text
        driver.find_element(*cancel_btn_locator).click()
        driver.find_element(*del_btn_locator).click()
# 恢复环境
        driver.find_element(*add_moedel_locator).click()
        wait = WebDriverWait(driver, 10, 0.5)
        wait.until(EC.presence_of_all_elements_located((By.ID, 'name')))
        driver.find_element(*model_name_locator).send_keys(get_model_name)
        Select(driver.find_element(*parent_class_locator)).select_by_visible_text(get_perent_class)
        driver.find_element(By.XPATH,'//*[@id="dialog-message1"]/form/div[3]/div/textarea').send_keys(get_descripe)
        driver.find_element(*submit_btn_locator).click()
        break
driver.quit()